<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>ASR-Node</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      #app {
        width: 1000px;
        margin: 100px auto 0;
        text-align: center;
      }
      h3 {
        margin-bottom: 20px;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h3>ChatGPT-ASR-Bot</h3>
      <main>
        <span>发送：</span>
        <span id="send_msg"></span>
        <br />
        <span>接收：</span>
        <span id="rec_msg"></span>
      </main>
    </div>
    <script type="module">
      import { catchAwait } from "../node_modules/utils-lib-js/dist/esm/index.js";
      import { init, elems, getLastMsg } from "./main.js";
      import { sendMsg } from "./api.js";
      const { send_msg, rec_msg } = elems;
      const { speechRec, speechSyn } = init();
      speechRec.result(async (event) => {
        const transcript = getLastMsg(event); // 获取识别结果
        send_msg.textContent = transcript;
        const [err, res] = await catchAwait(sendMsg(transcript)); // 发送到服务端
        const { content } = res?.choices?.[0]?.message;
        rec_msg.textContent = err ?? content;
        speechSyn.speak(err ?? content);
        console.info(transcript, res);
      });
      speechRec.end(() => {
        // 保持语音识别运行
        speechRec.reset();
      });
      speechRec.reset();
    </script>
  </body>
</html>
